<template>
  <div id="questionSubmitView">
    <a-card class="questionSubmitList">
      <a-form :model="searchParams" layout="inline">
        <a-form-item
          field="questionId"
          label="题号"
          style="width: 180px"
          class="flex-2"
        >
          <a-input
            v-model="searchParams.questionId"
            placeholder="请输入题目ID"
          />
        </a-form-item>
        <a-form-item
          field="userId"
          label="提交者ID"
          style="width: 180px"
          class="flex-2"
        >
          <a-input v-model="searchParams.userId" placeholder="请输入提交者ID" />
        </a-form-item>
        <a-form-item
          field="extent"
          label="编程语言"
          style="width: 180px"
          class="flex-2"
        >
          <a-select
            v-model="extent"
            :style="{ width: '320px' }"
            placeholder="全部"
          >
            <a-option style="color: var(--color-text-2)">全部</a-option>
            <a-option v-for="language in codeLanguages" :key="language">
              {{ language }}
            </a-option>
          </a-select>
        </a-form-item>
        <a-button
          type="outline"
          @click="doSubmit"
          :style="{ color: '#065ACC', borderColor: '#065ACC' }"
        >
          <icon-search style="margin-right: 2px" />搜索
        </a-button>
        <a-form-item>
          <a-button
            type="outline"
            @click="refresh"
            :style="{ color: '#27ae60', borderColor: '#27ae60' }"
          >
            <icon-refresh style="margin-right: 2px" />刷新
          </a-button>
        </a-form-item>
      </a-form>
      <a-divider :size="divederSize" />
      <a-table
        :columns="columns"
        :data="dataList"
        :pagination="{
          showTotal: true,
          pageSize: searchParams.pageSize,
          current: searchParams.current,
          total,
        }"
        @page-change="onPageChange"
        :bordered="{ wrapper: true, cell: true }"
        stripe
      >
        <template #result="{ record }">
          <a-tag
            v-if="record.judgeInfo.message === 'Accepted'"
            color="green"
            bordered
          >
            作答正确
          </a-tag>
          <a-tag v-else-if="record.statue === 1" color="blue" bordered>
            正在编译
          </a-tag>
          <a-tag
            v-else-if="record.judgeInfo.message === 'Wrong Answer'"
            color="red"
            bordered
          >
            作答错误
          </a-tag>
          <a-tag v-else color="gray" bordered> 编译失败 </a-tag>
        </template>
        <template #time="{ record }">
          <p class="runInfo" v-if="record.judgeInfo.time">
            {{ record.judgeInfo.time }}ms
          </p>
          <p class="runInfo" v-else>N/A</p>
        </template>
        <template #memory="{ record }">
          <p class="runInfo" v-if="record.judgeInfo.memory">
            {{ record.judgeInfo.memory }}KB
          </p>
          <p class="runInfo" v-else>N/A</p>
        </template>
        <template #createTime="{ record }">
          {{ dayjs(record.createTime).format("YYYY-MM-DD") }}
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { IconRefresh, IconSearch } from "@arco-design/web-vue/es/icon";
import {
  Question,
  QuestionSubmitQueryRequest,
  QuestionControllerService,
} from "../../../generated";
import { ref, onMounted, computed, watch, watchEffect } from "vue";
import dayjs from "dayjs";
import { Message } from "@arco-design/web-vue";
import "@arco-design/web-vue/es/message/style/css.js";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const store = useStore();
const router = useRouter();
const dataList = ref([]);
const total = ref(0);
const divederSize = 0;
const extent = ref("全部");
const codeLanguages = ref(["java", "cpp"]);
const searchParams = ref<QuestionSubmitQueryRequest>({
  status: undefined,
  userId: undefined,
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});

const pageSize = computed(() => searchParams.value.pageSize);
const current = computed(() => searchParams.value.current);

watch([pageSize, current], () => {
  loadData();
});
watchEffect(() => {
  if (extent.value === "全部") {
    searchParams.value.language = undefined;
  } else {
    searchParams.value.language = extent.value;
  }
});

const loadData = async () => {
  store.commit("loading/showLoading", true);
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    },
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    Message.error("加载失败，" + res.message);
  }
  store.commit("loading/showLoading", false);
};

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "Run ID",
    dataIndex: "id",
  },
  {
    title: "提交者 id",
    dataIndex: "userId",
  },
  {
    title: "题目 id",
    dataIndex: "questionId",
  },
  {
    title: "判题结果",
    slotName: "result",
  },
  {
    title: "运行时间",
    slotName: "time",
  },
  {
    title: "运行内存",
    slotName: "memory",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "提交时间",
    slotName: "createTime",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};
const doSubmit = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loadData();
};

const refresh = () => {
  searchParams.value = {
    status: undefined,
    userId: undefined,
    questionId: undefined,
    language: undefined,
    pageSize: 10,
    current: 1,
  };
  loadData();
};

/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};
</script>

<style scoped>
.flex-1 {
  flex: 1;
}
.flex-2 {
  flex: 2;
}
#questionSubmitView {
  max-width: 1440px;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
}
.questionSubmitList {
  flex: 1;
  margin-right: 8px;
}
</style>
